package com.creative.accountsdk.holder;

import android.app.Dialog;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.creative.accountsdk.AccountSdk;
import com.creative.accountsdk.AccountUtils;
import com.creative.accountsdk.DataCenter;
import com.creative.accountsdk.DialogUtils;
import com.creative.accountsdk.OnLoginListener;
import com.creative.accountsdk.R;
import com.creative.accountsdk.UserData;
import com.creative.accountsdk.net.XHttpHandler;
import com.creative.accountsdk.net.account.FindPasswordProtocol;
import com.creative.accountsdk.net.account.RegisteProtocol;
import cz.msebera.android.httpclient.Header;
import org.json.JSONObject;

/**
 * Created by yinjialiang on 15-6-23.
 */
public class FindPasswordHolder extends BaseHolder{
    private EditText editTextUsername;

    @Override
    protected void initViews() {
        editTextUsername = (EditText) viewRoot.findViewById(R.id.editTextUsername);

        viewRoot.findViewById(R.id.buttonOK).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                find();
            }
        });
    }

    private void find()
    {
        final String username = editTextUsername.getText().toString().trim();
        if (username.equals("")) {
            Toast.makeText(activity, R.string.account_username_empty, Toast.LENGTH_SHORT).show();
        }
        else if(!AccountUtils.isValidEmail(username))
        {
            Toast.makeText(activity, R.string.account_invalid_email, Toast.LENGTH_SHORT).show();
        }
        else if (username.length() < 6) {
            Toast.makeText(activity, R.string.account_username_length, Toast.LENGTH_SHORT).show();
        }
        else {
            final Dialog progress = DialogUtils.createProgress(activity);
            progress.show();
            final FindPasswordProtocol protocol = new FindPasswordProtocol(username);
            protocol.debugUrl(DataCenter.getHttpClient());
            DataCenter.getHttpClient().get(protocol.getUrl(), protocol.getParams(), new XHttpHandler(activity, progress) {
                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                    super.onSuccess(statusCode, headers, response);
                    progress.cancel();
                    if (protocol.parse(response, activity)) {
                        activity.popupHolder();
                        activity.visibleHolder();
                        String text = activity.getResources().getText(R.string.reset_password_link)
                                + username
                                + activity.getResources().getText(R.string.reset_password_link2);
                        Toast.makeText(activity, text, Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }

    @Override
    public int getLayoutResID() {
        return R.layout.account_find_password;
    }
}
